package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.SiteMatMapper;
import com.indusfo.spc.mapper.UploadMapper;
import com.indusfo.spc.pojo.Mat;
import com.indusfo.spc.pojo.Site;
import com.indusfo.spc.pojo.SiteMat;
import com.indusfo.spc.service.SiteMatService;
import com.indusfo.spc.vo.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class SiteMatServiceImpl implements SiteMatService {

    private  static  final Logger logger = LoggerFactory.getLogger(SiteMatServiceImpl.class);

    @Resource
    private SiteMatMapper siteMatMapper;
    @Resource
    private UploadMapper uploadMapper;


    @Override
    public JSONObject selectSiteMat(SiteMat siteMat) {
        List<SiteMat> siteMatList = null;
        int count = 0;
        try {
            Integer pagesize = siteMat.getPagesize();
            Integer pageindex = siteMat.getPageindex();
            if(pagesize != null && pageindex != null) {
                siteMat.setIncept(pagesize*(pageindex - 1));
            }
            if(siteMat.getSiteId() == null) {
                throw new ParamsErrorException("请选择库位");
            }
            siteMatList = siteMatMapper.selectSiteMat(siteMat);
            //查询分页总记录数
            count = siteMatMapper.countSiteMat(siteMat); //返回查询到的总记录数
            if (siteMatList.isEmpty()) {
                return JSONObject.oK("没有查询到相关数据", siteMatList, count);
            }
            return JSONObject.oK("查询成功", siteMatList, count);
        } catch (
                GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    @Override
    public JSONObject insertSiteMat(SiteMat siteMat) {
        try {
            if(siteMat.getSiteMatId()!= null) {
                throw new ParamsErrorException("新增时，不能填写id");
            }
            checkParam(siteMat);
            // 调用存储过程
            int row = siteMatMapper.insertSelective(siteMat);
            if(row == 0) {
                throw new ModifyFailedException("数据新增失败");
            }
            return JSONObject.oK("新增成功");
        } catch (GlobalException e) {  //这里只捕获自定义异常
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    @Override
    public JSONObject updateSiteMat(SiteMat siteMat) {
        try {
            if(siteMat.getSiteMatId()== null) {
                throw new ParamsErrorException("请选择要更新的");
            }
            checkParam(siteMat);
            // 调用存储过程
            int row = siteMatMapper.updateByPrimaryKeySelective(siteMat);
            if(row == 0) {
                throw new ModifyFailedException("数据编辑失败");
            }
            return JSONObject.oK("编辑成功");
        } catch (GlobalException e) {  //这里只捕获自定义异常
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    @Override
    public JSONObject definedSiteMat(Long[] siteMatIds, Integer dataState) {
        try {
            if(dataState == null) {
                throw new ParamsErrorException("数据状态不能为空");
            }else if(dataState != 1 && dataState != 2 && dataState != 3) {
                throw new ParamsErrorException("数据状态错误");
            }
            if (siteMatIds == null) {
                throw new ParamsErrorException("id不能为空");
            }
            // 执行存储过程
            int row = siteMatMapper.definedSiteMat(siteMatIds, dataState);
            String msg = "";
            if (row == 0) {
                // 判断传入的数据状态参数lDataState,返回相应信息
                switch (dataState) {
                    case 1:
                        throw new ModifyFailedException("启用失败");
                    case 2:
                        throw new ModifyFailedException("删除失败");
                    case 3:
                        throw new ModifyFailedException("停用失败");
                    default:
                }
            } else {
                switch (dataState) {
                    case 1:
                        msg = "启用成功";break;
                    case 2:
                        msg = "删除成功";break;
                    case 3:
                        msg = "停用成功";break;
                    default:
                }
            }
            return JSONObject.oK(msg);
        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    @Override
    public String checkNoRepeatExcel(SiteMat siteMat) {
        //物料名称
        String matCode = siteMat.getMatCode();
        //最小库存
        String siteMatNum = siteMat.getSiteMatNum();
        //库位名称
        String siteName = siteMat.getSiteName();
        if(StringUtils.isEmpty(matCode) && StringUtils.isEmpty(siteMatNum) && StringUtils.isEmpty(siteName)){
            return "该行内容为空/删除不彻底";
        }
        //判断物料名称是否正确
        Map<String, Mat> matMap = uploadMapper.queryMat();
        if(!matMap.containsKey(matCode)){
            return "物料名称填写错误/不规范";
        }
        siteMat.setMatId(matMap.get(matCode).getMatId());
        //判断库存名称是否正确
        if(StringUtils.isEmpty(siteName)){
            return "库存名称为空";
        }

        Map<String, Site> siteMap = uploadMapper.querySite();
        if(!siteMap.containsKey(siteName)){
            return "库存名称填写错误/不规范";
        }
        siteMat.setSiteId(siteMap.get(siteName).getSiteId());
        if(StringUtils.isEmpty(siteMatNum)){
            return "安全库存不能为空";
        }
        if (siteMatNum.getBytes().length > 10) {
            return "安全库存不能超过10位数";
        }
        // 库位 物料
        Integer depCounts = siteMatMapper.querySiteMat(siteMat);
        if (depCounts !=0) {
           return "该物料已存在";
        }
        return null;
    }

    @Override
    public JSONObject updateListSiteMat(List<SiteMat> siteMatList) {
        uploadMapper.updateListSiteMat(siteMatList);
        return null;
    }

    /**
     * 参数校验
     */
    private void checkParam(SiteMat siteMat) {
        if (siteMat.getSiteId() == null) {
            throw new ParamsErrorException("请选择库位");
        }
        if (siteMat.getMatId() == null) {
            throw new ParamsErrorException("请选择物料");
        }
        if (StringUtils.isEmpty(siteMat.getSiteMatNum())) {
            throw new ParamsErrorException("请填写数量");
        }
        if (siteMat.getSiteMatNum().getBytes().length > 50) {
            throw new ParamsErrorException("库位编码超过规定,长度50");
        }
        // 库位 物料
        Integer depCounts = siteMatMapper.querySiteMat(siteMat);
        if (depCounts !=0) {
            throw new ParamsErrorException("该物料已存在");
        }
    }

}
